package com.google.samples.apps.library_common.utils.glide

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.hardware.display.DisplayManager
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.ImageViewTarget
import com.google.samples.apps.library_common.R


/**
 * Created by xuhao on 2017/11/27.
 * desc:
 */
object ImageLoaderUtils {
    val defaultAvatar = R.drawable.shape_default_avatar

    init {

    }

    fun getDefaultRequestOptions(): RequestOptions {
        return RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
//                .placeholder(R.drawable.placeholder)
                .error(R.drawable.shape_placeholder)
    }

    fun getAvatarDefaultRequestOptions(): RequestOptions {
        return RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .placeholder(defaultAvatar)
                .error(defaultAvatar)
    }

    fun displayDefault(imageView: ImageView, url: String?) {
        Glide.with(imageView.context)
                .load(url)
                .apply(
                        RequestOptions()
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                )
                .into(imageView)
    }


    fun displayFillet(imageView: ImageView, url: String?, radius: Int) {
        Glide.with(imageView.context).load(url)
                .apply(getDefaultRequestOptions())
                .centerCrop()
                .transform(RoundedCornersTransformation(radius, 0))
                // .thumbnail(0.1f)
//                .centerCrop()
                .into(imageView)

    }


    fun display(imageView: ImageView, url: String?) {
        Glide.with(imageView.context)
                .load(url)
                .apply(getDefaultRequestOptions())
                .centerCrop()
                .transition(DrawableTransitionOptions().crossFade())
                // .thumbnail(0.1f)
                .into(imageView)
    }


    fun display(imageView: ImageView, url: String?, isCahe: Boolean) {
        val defaultRequestOptions = getDefaultRequestOptions()
        if (!isCahe) {
            defaultRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
        }
        Glide.with(imageView.context)
                .load(url)
                .apply(defaultRequestOptions)
                .centerCrop()
                .transition(DrawableTransitionOptions().crossFade())
                // .thumbnail(0.1f)
                .into(imageView)
    }


    fun displayXY(imageView: ImageView, url: String?) {
        Glide.with(imageView.context)
                .load(url)
//                .apply(RequestOptions().placeholder(R.drawable.placeholder))
//                .centerCrop()
                .transition(DrawableTransitionOptions().crossFade())
                // .thumbnail(0.1f)
                .into(imageView)
    }


    fun display(imageView: ImageView, res: Int) {
        Glide.with(imageView.context)
                .load(res)
//                .apply(RequestOptions().placeholder(R.drawable.placeholder))
                .centerCrop()
                .transition(DrawableTransitionOptions().crossFade())
                // .thumbnail(0.1f)
                .into(imageView)
    }


    fun displayAvatar(imageView: ImageView, url: String?) {
        Glide.with(imageView.context)
                .load(url)
                .apply(getAvatarDefaultRequestOptions())
                .centerCrop()
                .transition(DrawableTransitionOptions().crossFade())
                .transform(RoundedCornersTransformation(Int.MAX_VALUE, 0))
                // .thumbnail(0.1f)
                .into(imageView)
    }

    fun displayAvatar(imageView: ImageView, res: Int?) {
        Glide.with(imageView.context)
                .load(res)
                .apply(getAvatarDefaultRequestOptions())
                .centerCrop()
                .transition(DrawableTransitionOptions().crossFade())
                .transform(RoundedCornersTransformation(Int.MAX_VALUE, 0))
                // .thumbnail(0.1f)
                .into(imageView)
    }


}